package trunk.graph;

public class Edge implements Comparable<Edge> {

	private Vertex v1;
	private Vertex v2;
	private double weight;

	public Edge(Vertex v1, Vertex v2) {
		this.v1 = v1;
		this.v2 = v2;
	}
	public Edge(Vertex v1, Vertex v2, double weight) {
		this.v1 = v1;
		this.v2 = v2;
		this.weight = weight;
	}
	
	public Vertex getV1() {
		return v1;
	}
	
	public Vertex getV2() {
		return v2;
	}
	
	public double getWeight() {
		return weight;
	}
	
	/**
	 * set vertices, use null to leave a vertex unchanged
	 * @param v1
	 * @param v2
	 */
	public void setVertices(Vertex v1, Vertex v2) {
		if(v1 != null)
			this.v1 = v1;
		if(v2 != null)
			this.v2 = v2;
	}
	
	public void setWeight(double weight) {
		this.weight = weight;
	}
	
	@Override
	public boolean equals(Object obj) {
        try {
                Edge otherEdge = (Edge) obj;
                boolean result = (v1.equals(otherEdge.getV1()) && v2.equals(otherEdge.getV2()));
                result = result || (v1.equals(otherEdge.getV2()) && v2.equals(otherEdge.getV1()));
                return result;
        } catch (Exception e) {
                return super.equals(obj);
        }
	}

	@Override
	public int compareTo(Edge e) {

		if(weight > e.getWeight())
			return 1;
		if(weight < e.getWeight())
			return -1;
		return 0;
	}
	
	@Override
	public String toString() {
		return v1 + " - " + weight + " - " + v2;
	}
}
